home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-27 | 7.5 KB | 412 lines | [TEXT/SNIJ] |
- /*
- Edit.java
-
- Really simple Java text editor.
-
- © 1997 by Michael J. Webb (mjw@codewell.com)
-
- */
-
- /*
- This class is an extension of the Frame class for use as the
- main window of an application.
- */
-
- import java.awt.*;
-
- import java.lang.Long;
-
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
-
- public class Edit extends Frame
- {
-
- /* Construction and Destruction Methods. */
-
- // Default constructor. Builds the window layout.
- public Edit()
- {
- super();
-
- //{{INIT_MENUS
- menuBar = new java.awt.MenuBar();
-
- fileMenu = new java.awt.Menu("File");
- fileMenu.add("New");
- fileMenu.add("Open…");
- fileMenu.addSeparator();
- fileMenu.add("Close");
- fileMenu.add("Save");
- fileMenu.add("Save As…");
- fileMenu.addSeparator();
- fileMenu.add("Quit");
- menuBar.add(fileMenu);
-
- editMenu = new java.awt.Menu("Edit");
- editMenu.add("Undo");
- editMenu.addSeparator();
- editMenu.add("Cut");
- editMenu.add("Copy");
- editMenu.add("Paste");
- menuBar.add(editMenu);
- setMenuBar(menuBar);
- //}}
-
- //{{INIT_CONTROLS
- GridBagLayout gridBagLayout;
- gridBagLayout = new GridBagLayout();
- setLayout(gridBagLayout);
- resize(insets().left + insets().right + 558,insets().top + insets().bottom + 441);
- panel1 = new java.awt.Panel();
- panel1.setLayout(null);
- panel1.reshape(insets().left + 0,insets().top + 0,125,441);
- panel1.setBackground(new Color(12632256));
- GridBagConstraints gbc;
- gbc = new GridBagConstraints();
- gbc.gridx = 0;
- gbc.gridy = 0;
- gbc.weighty = 1;
- gbc.anchor = GridBagConstraints.NORTHWEST;
- gbc.fill = GridBagConstraints.VERTICAL;
- gbc.insets = new Insets(0,0,0,0);
- gridBagLayout.setConstraints(panel1, gbc);
- add(panel1);
- button1 = new java.awt.Button("Open...");
- button1.reshape(10,10,80,23);
- button1.setBackground(new Color(16777215));
- panel1.add(button1);
- button2 = new java.awt.Button("Save");
- button2.reshape(10,40,80,23);
- button2.setBackground(new Color(16777215));
- panel1.add(button2);
- panel2 = new java.awt.Panel();
- panel2.setLayout(new BorderLayout(0,0));
- panel2.reshape(insets().left + 125,insets().top + 0,433,441);
- panel2.setForeground(new Color(0));
- panel2.setBackground(new Color(16777215));
- gbc = new GridBagConstraints();
- gbc.gridx = 2;
- gbc.gridy = 0;
- gbc.weightx = 1;
- gbc.weighty = 1;
- gbc.anchor = GridBagConstraints.NORTHEAST;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.insets = new Insets(0,0,0,0);
- gridBagLayout.setConstraints(panel2, gbc);
- add(panel2);
- textArea1 = new java.awt.TextArea();
- textArea1.reshape(0,0,433,441);
- textArea1.setFont(new Font("Helvetica", Font.PLAIN, 10));
- panel2.add("Center", textArea1);
- setTitle("Untitled");
- //}}
-
- show();
- }
-
- /* Public Methods. */
-
- // Show the window.
- public synchronized void show()
- {
- move(50, 50);
- super.show();
- }
-
- // Specific event handler.
- public boolean handleEvent(Event event)
- {
- if (event.id == Event.WINDOW_DESTROY)
- {
- hide(); // hide the Frame
- dispose(); // tell windowing system to free resources
- return true;
- }
-
- if (event.target == button1 && event.id == Event.ACTION_EVENT)
- {
- button1_Clicked(event);
- return true;
- }
-
- if (event.target == button2 && event.id == Event.ACTION_EVENT)
- {
- button2_Clicked(event);
- return true;
- }
-
- return super.handleEvent(event);
- }
-
- // The big action switch.
- public boolean action(Event event, Object arg)
- {
- if (event.target instanceof java.awt.MenuItem)
- {
- String Label = (String) arg;
-
- if (Label.equalsIgnoreCase("Quit"))
- {
- System.exit(0);
- }
- else if (Label.equalsIgnoreCase("Open…"))
- {
- selectedOpen();
- return true;
- }
- else if (Label.equalsIgnoreCase("New"))
- {
- selectedNew();
- return true;
- }
- else if (Label.equalsIgnoreCase("Close"))
- {
- selectedClose();
- return true;
- }
- else if (Label.equalsIgnoreCase("Save"))
- {
- selectedSave();
- return true;
- }
- else if (Label.equalsIgnoreCase("Save As..."))
- {
- selectedSaveAs();
- return true;
- }
- }
-
- return super.action(event, arg);
- }
-
- // Work routine to open files.
- public static void openFile (Edit app, File theFile)
- {
- FileInputStream fileStream = null;
- DataInputStream dataStream = null;
-
- try
- {
- fileStream = new FileInputStream(theFile);
- dataStream = new DataInputStream(fileStream);
-
- app.fFile = theFile;
- app.restore(dataStream);
- app.setTitle(theFile.getName());
-
- }
- catch (Throwable e)
- {
- if (app != null)
- {
- app.selectedClose();
- }
-
- System.err.println(e.toString());
- }
- finally
- {
- try
- {
- if (dataStream != null) dataStream.close();
- }
- catch (Throwable e)
- {
- }
-
- try
- {
- if (fileStream != null) fileStream.close();
- }
- catch (Throwable e)
- {
- }
- }
- }
-
- // Called to choose a file to open.
- public synchronized void selectedOpen()
- {
- Edit app;
- FileDialog fileDialog = new FileDialog(this, "Open…");
- fileDialog.show();
-
- fFile =
- new File
- (
- fileDialog.getDirectory(),
- fileDialog.getFile()
- );
-
- app = new Edit();
- openFile(app, fFile);
- }
-
- // Create a new window.
- public void selectedNew()
- {
- Edit app = new Edit();
- }
-
- // Close a window.
- public void selectedClose()
- {
- hide(); // hide the Frame
- dispose(); // tell windowing system to free resources
- }
-
- // Save a file.
- public synchronized void selectedSave()
- {
- FileOutputStream fileStream = null;
- PrintStream printStream = null;
-
- if (fFile == null)
- {
- FileDialog fileDialog;
-
- fileDialog =
- new FileDialog
- (
- this,
- "Save",
- java.awt.FileDialog.SAVE
- );
-
- fileDialog.show();
-
- fFile =
- new File
- (
- fileDialog.getDirectory(),
- fileDialog.getFile()
- );
- }
-
- try
- {
- fileStream = new FileOutputStream(fFile);
- printStream = new PrintStream(fileStream);
- store(printStream);
- }
- catch (Throwable e)
- {
- System.err.println(e.toString());
- }
- finally
- {
- try
- {
- if (printStream != null) printStream.close();
- }
- catch (Throwable e)
- {
- }
-
- try
- {
- if (fileStream != null) fileStream.close();
- }
- catch (Throwable e)
- {
- }
- }
- }
-
- // Save a file as the given file.
- public void selectedSaveAs()
- {
- fFile = null;
- selectedSave();
- }
-
- // Restore from a stream.
- public void restore(DataInputStream stream)
- {
- try
- {
- String textLine;
-
- textLine = stream.readLine();
-
- while (textLine != null)
- {
- textArea1.appendText(textLine);
- textArea1.appendText("\n");
- textLine = stream.readLine();
- }
- }
- catch (Throwable e)
- {
- }
- }
-
- // Store to a stream.
- public void store(PrintStream stream)
- {
- stream.println(textArea1.getText());
- }
-
- /* Public Static Methods. */
-
- // Application entry point.
- public static void main(String args[])
- {
- if (args.length == 0)
- {
- Edit app;
- app = new Edit();
- }
- else
- {
- File theFile;
- Edit app;
-
- for (int i = 0; i < args.length; i++)
- {
- theFile = new File(args[i]);
-
- if (theFile.canRead())
- {
- app = new Edit();
- openFile(app, theFile);
- }
- }
- }
- }
-
- /* Private Members. */
-
- File fFile = null;
-
- //{{DECLARE_MENUS
- java.awt.MenuBar menuBar;
- java.awt.Menu fileMenu;
- java.awt.Menu editMenu;
- //}}
-
- //{{DECLARE_CONTROLS
- java.awt.Panel panel1;
- java.awt.Button button1;
- java.awt.Button button2;
- java.awt.Panel panel2;
- java.awt.TextArea textArea1;
- //}}
-
- // Open button event handler.
- void button1_Clicked(Event event)
- {
- selectedOpen();
- }
-
- // Save button event handler.
- void button2_Clicked(Event event)
- {
- selectedSave();
- }
- }
-